package leetcode.code0917;

public class Solution {
	public String reverseOnlyLetters(String s) {
		char[] cs = s.toCharArray();
		int len = cs.length;
		char[] ans = new char[len];
		int l = 0, r = len - 1;
		char L = '0';
		while (r >= 0) {
			char R = cs[r];
			if ((R >= 'a' && R <= 'z') || (R >= 'A' && R <= 'Z')) {
				while (l < len && ((L = cs[l]) < 'a' || L > 'z') && (L < 'A' || L > 'Z')) {
					l++;
				}
				ans[r] = L;
				l++;
			} else {
				ans[r] = R;
			}
			r--;
		}
		return new String(ans);
	}

}
